<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=0,shrink-to-fit=no, viewport-fit=cover">
    <meta name="keywords" content="蓝桥到家">
    <meta name="description" itemprop="description" content="生鲜副食、饮品乳品、鲜花蛋糕">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>在线支付</title>
    <link rel="stylesheet" type="text/css" href="../styles/reset.css" />
    <link rel="stylesheet" type="text/css" href="../styles/iconfont/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="../styles/payment.css" />
    <link rel="stylesheet" type="text/css" href="../styles/common.css" />
    <script type="text/javascript" src="../js/common.js"></script>
    <script src="../js/zepto.min.js"></script>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script>
        window.onload = function () {
            const {createApp, ref,computed} = Vue;

            createApp({
                setup(){
                    const shopCarList = ref([])
                    const deliveryFee = 6 //配送费固定

                    // 2. 获取数据后初始化count（确保每个商品有数量属性）
                    let getData = () => {
                        fetch(`http://localhost:3000/shopCar`)
                            .then(response => response.json())
                            .then(data => {
                                shopCarList.value = data[0]
                            })
                            .catch(err => console.error('获取数据失败:', err));
                    }
                    getData()

                    const sumPrice =  computed(() =>{
                        let sum = deliveryFee;
                        for (let i = 1; i < shopCarList.value.shopCarGoods.length+1; i++) {
                            sum = Number.parseFloat((shopCarList.value.shopCarGoods[i-1].count * shopCarList.value.shopCarGoods[i-1].price).toFixed(2)) + sum;
                        }
                        return sum;
                    })

                    let newStatus = (orderID) =>{
                        fetch(`http://localhost:3000/shopCar/${orderID}`,{
                            method:"PATCH",
                            headers:{
                                'Content-Type': 'application/json'
                            },
                            body:JSON.stringify({orderStatus:"配送中"})
                        })
                    }

                    let confirmPay = () =>{
                        newStatus(shopCarList.value.id)
                        window.location.href="order.html"
                    }

                    return {shopCarList,sumPrice,deliveryFee,confirmPay}
                }
            }).mount('#app')
        }
    </script>
</head>

<body>
    <div class="wrap" id="app">
        <!-- 头部 start -->
        <header class="header_bar">
            <a href="order-confirm.html">
                <div class="header_bar_back"></div>
            </a>
            <div class="header_bar_title">在线支付</div>
        </header>
        <!-- 头部 end -->
        <div class="container">
            <!-- 订单信息部分 -->
            <div class="order-info">
                <p>
                    永辉超市
                    <i class="iconfont icon-xiajiantou" id="btnshow" style="font-size: .3rem;"></i>
                </p>
                <p>&#165; {{ sumPrice }}</p>
            </div>

            <!-- 订单明细部分 -->
            <ul class="order-detail" id="detailetBox">
                <li  v-for = "(item,index) in shopCarList.shopCarGoods" :key="index">
                    <p>{{ item.names }} x {{ item.count }}</p>
                    <p>&#165;{{ (item.price * item.count).toFixed(2) }}</p>
                </li>
                <li>
                    <p>配送费</p>
                    <p>&#165;{{ deliveryFee }}</p>
                </li>
            </ul>

            <!-- 支付方式部分 -->
            <ul class="payment-type">
                <li>
                    <img src="../images/common/alipay.png">
                </li>
                <li>
                    <img src="../images/common/wechat.png">
                </li>
            </ul>
            <div class="payment-button" @click="confirmPay">
                <button>确认支付</button>
            </div>

        </div>
    </div>
</body>

</html>